package com.siao.mvc
{
	import com.siao.applications.Module;
	import com.siao.data.ArrayList;
	import com.siao.mvc.core.IContext;
	import com.siao.mvc.core.IState;
	import com.siao.mvc.core.Node;
	import com.siao.mvc.managers.Controllers;
	import com.siao.mvc.managers.ModelManager;
	
	import flash.display.DisplayObjectContainer;
	import flash.system.ApplicationDomain;
	import flash.utils.getQualifiedClassName;
	
	/**
	 * 状态
	 * @author SiaoLeon
	 */
	public class State extends Node implements IState, IContext
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		private var _key:Object;
		
		public function get key():Object
		{
			return this._key;
		}
		
		private var _module:Module;
		
		/**
		 * 状态所属模块
		 * @return
		 */
		public function get module():Module
		{
			return this._module;
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var views:ArrayList;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function State(key:Object=null)
		{
			this._key = (key == null) ? ApplicationDomain.currentDomain.getDefinition(getQualifiedClassName(this)) : key;
			
			this.views = new ArrayList();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		public function addModel(model:Class):void
		{
			ModelManager.instance.addContext(this, model);
		}
		
		public function getModel(model:Class):Model
		{
			return ModelManager.instance.getModel(model);
		}
		
		public function removeModel(model:Class):void
		{
			ModelManager.instance.removeContext(this, model);
		}
		
		public function clearModel():void
		{
			ModelManager.instance.removeContext(this);
		}
		
		public function addView(view:View, container:DisplayObjectContainer=null):void
		{
			if (this.views.contains(view))
			{
				return;
			}
			
			view._context = this;
			view.onAdd();
			
			if (container != null)
			{
				container.addChild(view);
			}
			else if (this.module != null)
			{
				this.module.addChild(view);
			}
			
			this.views.add(view);
		}
		
		public function removeView(view:View):void
		{
			if (!this.views.contains(view))
			{
				return;
			}
			
			view.onRemove();
			view.remove();
			this.views.remove(view);
			view._context = null;
			view = null;
		}
		
		public function clearView():void
		{
			while (this.views.length > 0)
			{
				var view:View = this.views.element(0);
				this.removeView(view);
			}
		}
		
		public function addController(controller:Controller):void
		{
			controller._context = this;
			Controllers.instance.add(controller);
			controller.onAdd();
		}
		
		public function removeController(controller:Controller):void
		{
			controller.onRemove();
			Controllers.instance.remove(controller);
			controller._context = null;
			controller = null;
		}
		
		public function clearController():void
		{
			var list:ArrayList = Controllers.instance.getControllers(this);
			
			if (list == null)
			{
				return;
			}
			
			while (list.length > 0)
			{
				this.removeController(list.element(0) as Controller);
			}
		}
		
		/**
		 * 初始化该状态时处理函数
		 */
		public function onInit(module:Module):void
		{
			this._module = module;
		}
		
		/**
		 * 进入该状态时处理函数
		 */
		public function onEnter():void
		{
			if (this.module != null)
			{
				for each (var view:View in this.views.toArray())
				{
					this.module.addChild(view);
				}
			}
		}
		
		/**
		 * 退出该状态时处理函数
		 */
		public function onExit():void
		{
			this.clearModel();
			this.clearView();
			this.clearController();
			this._module = null;
		}
	
		// -------------------------------------------------------------------------------------------------------- 函数
	
	}
}
